<template>
	<view>
		<u-navbar title="一键加油站" :border-bottom='false' backgroundColor='#fff' title-color='#333' back-icon-color='#333'>
			<view slot="right" style="padding-right: 32rpx;" @click="navigateTo('./fillOrder/fillOrder')">
				<u-icon name="account" size="40" color='#218BFF'></u-icon>
			</view>
		</u-navbar>
		<u-dropdown ref="uDropdown">
			<u-dropdown-item :title="city" disabled></u-dropdown-item>
			<u-dropdown-item v-model="value2" :title="title2">
				<view class="slot-content" style="background-color: #FFFFFF;">
					<view class="lable">
						汽油
					</view>
					<view class="typeList">
						<view class="typeItem" :class="{active:qyitem.isCheck == 1}" v-for="(qyitem,qyx) in qylist"
							:key='qyx' @click="choseQy(qyx)">
							{{qyitem.text}}
						</view>
					</view>
					<view class="lable">
						柴油
					</view>
					<view class="typeList">
						<view class="typeItem" :class="{active:cyitem.isCheck == 1}" v-for="(cyitem,cyx) in cylist"
							:key='cyx' @click="choseCy(cyx)">
							{{cyitem.text}}
						</view>
					</view>
				</view>
			</u-dropdown-item>
			<u-dropdown-item v-model="value3" :title="title3" :options="options3" @change="dropdownFn1">
			</u-dropdown-item>
			<u-dropdown-item v-model="value4" :title="title4">
				<view class="slot-content" style="background-color: #FFFFFF;">
					<scroll-view scroll-y="true" style="height: 540rpx;">
						<view class="typeList">
							<view class="typeItem" style="font-size: 24rpx;" :class="{active:banx== curBan}"
								v-for="(ban,banx) in brandList" :key='banx' @click="choseBan(banx)">
								{{ban.gas_brand_name}}
							</view>
						</view>
					</scroll-view>
				</view>
			</u-dropdown-item>
		</u-dropdown>

		<view class="station-list" v-if="stationList.length">
			<view class="station-item" @click="toDeatil(station)" v-for="(station,stationx) in stationList"
				:key='stationx'>
				<image :src="station.gas_logo_big" class="image"></image>
				<view class="info">
					<view class="title">{{station.gas_name}}</view>
					<view class="address">{{station.gas_address}}</view>
					<view class="detail" v-for="(item,index) in station.oil_price_list" :key='index' v-if="item.oilName == title2">
						<view class="price">￥{{item.priceYfq}}</view>
						<view class="youhui">
							<image class="icon" src="../../../static/imgs/jiangjiaicon.png"></image>
							<view class="youhui-price">直降{{(item.priceOfficial-item.priceYfq).toFixed(2)}}
							</view>
						</view>
						<view class="huaxian">国际价 ¥{{item.priceOfficial}}</view>
					</view>
				</view>
				<view class="to">
					<view class="btn"
						@click.stop="toMap(station.gas_address_latitude,station.gas_address_longitude,station.gas_name)">
						导航</view>
					<text>{{station.distance}}km</text>
				</view>
			</view>
		</view>

		<view class="tips" v-else style="width:100%;padding-top: 434rpx;">
			<u-empty text="暂无油站" mode="list"></u-empty>
		</view>

	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	export default {
		data() {
			return {
				curQy: 0,
				curCy: 0,
				curBan: 0,
				qylist: [{
						text: '92#',
						isCheck: 1,
						oilno:92
					},
					{
						text: '90#',
						isCheck: 0,
						oilno:90
					},
					{
						text: '95#',
						isCheck: 0,
						oilno:95
					},
					{
						text: '98#',
						isCheck: 0,
						oilno:98
					},
					{
						text: '101#',
						isCheck: 0,
						oilno:99
					},
					{
						text: 'LNG',
						isCheck: 0,
						oilno:15
					},
					{
						text: 'CNG',
						isCheck: 0,
						oilno:16
					}
				],
				cylist: [{
						text: '-10#',
						isCheck: 0,
						oilno:-10
					},
					{
						text: '-20#',
						isCheck: 0,
						oilno:-20
					},
					{
						text: '-30#',
						isCheck: 0,
						oilno:-30
					},
					{
						text: '-35#',
						isCheck: 0,
						oilno:-35
					},
					{
						text: '0#',
						isCheck: 0,
						oilno:0
					},
					{
						text: '国四0#',
						isCheck: 0,
						oilno:-1
					}
				],
				value2: 1,
				value3: 1,
				value4: 1,
				title2: '92#',
				title3: '智能排序',
				title4: '品牌优选',
				options3: [{
						label: '智能排序',
						value: 1,
					},
					{
						label: '距离优先',
						value: 2,
					},
					{
						label: '价格优先',
						value: 3,
					},
				],
				lat: '', // 经度
				lng: '', // 纬度
				page: '1',
				range: '50', // 范围
				oil_no: '92', // 油品
				order: '0', // 排序
				gas_type: '0', // 品牌
				stationList: [],
				brandList: [],
				city: ''
			}
		},
		onLoad() {
			let that = this
			// 获经当前纬度
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success(res) {
					that.lat = res.latitude
					that.lng = res.longitude
					that.city = res.address.city
					that.getStation(that.lat, that.lng, that.range, that.oil_no, that.gas_type, that.order)
				}
			})
			this.getBrand()
		},
		onReachBottom() {
			let page = Number(this.page) + 1
			this.page = page.toString()
			this.getStation(this.lat, this.lng, this.range, this.oil_no, this.gas_type, this.order)
		},
		methods: {
			dropdownFn1(e) {
				this.page = '1'
				this.stationList = []
				this.title3 = e
				if (e == 1) {
					this.title3 = '智能排序'
					this.order = 0
				}
				if (e == 2) {
					this.title3 = '距离优先'
					this.order = 1
				}
				if (e == 3) {
					this.title3 = '价格优先'
					this.order = 2
				}
				this.getStation(this.lat, this.lng, this.range, this.oil_no, this.gas_type, this.order)
			},
			choseQy(qyx) {
				this.page = '1'
				this.stationList = []
				this.qylist.forEach(item => {
					item.isCheck = 0
				})
				this.cylist.forEach(citem => {
					citem.isCheck = 0
				})
				this.qylist[qyx].isCheck = 1
				this.title2 = this.qylist[qyx].text
				this.oil_no = this.qylist[qyx].oilno
				this.$refs.uDropdown.close()
				this.getStation(this.lat, this.lng, this.range, this.oil_no, this.gas_type, this.order)
			},
			choseCy(cyx) {
				this.page = '1'
				this.stationList = []
				this.qylist.forEach(item => {
					item.isCheck = 0
				})
				this.cylist.forEach(citem => {
					citem.isCheck = 0
				})
				this.cylist[cyx].isCheck = 1
				this.title2 = this.cylist[cyx].text
				this.oil_no = this.cylist[cyx].oilno
				this.$refs.uDropdown.close()
				this.getStation(this.lat, this.lng, this.range, this.oil_no, this.gas_type, this.order)
			},
			choseBan(banx) {
				this.page = '1'
				this.stationList = []
				this.curBan = banx
				this.title4 = this.brandList[banx].gas_brand_name
				this.$refs.uDropdown.close()
				this.gas_type = this.brandList[banx].gas_brand_type
				this.getStation(this.lat, this.lng, this.range, this.oil_no, this.gas_type, this.order)
			},
			toDeatil(val) {
				// console.log(val)
				uni.setStorage({
					key: 'detail',
					data: val,
					success: function() {
						uni.navigateTo({
							url: './detail/detail'
						})
					}
				})
			},
			// 获取油站品牌
			async getBrand() {
				let res = await myRequest.getbrand()
				if (res.status) {
					this.brandList = res.data
				}
			},
			// 获取油站列表
			async getStation(lat, lng, range, oil_no, gas_type, order) {
				let {
					page
				} = this
				let res = await myRequest.getStationlist({
					page: page,
					lat: lat.toString(),
					lng: lng.toString(),
					range: range.toString(),
					oil_no: oil_no.toString(),
					gas_type: gas_type,
					order: order.toString(),
				})
				if (res.status) {
					this.stationList = [...this.stationList, ...res.data]
					console.log(this.stationList)
					if (res.data.length == 0) {
						return
					}
				}
			},

			navigateTo(url) {
				uni.navigateTo({
					url:url
				})
			},
			toMap(latitude, longitude, name) {
				let url = "";
				if (plus.os.name == "Android") { //判断是安卓端
					plus.nativeUI.actionSheet({ //选择菜单
						title: "选择地图应用",
						cancel: "取消",
						buttons: [{
							title: "百度地图"
						}, {
							title: "高德地图"
						}],
					}, function(e) {
						switch (e.index) {
							case 1:
								url =
									`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
								break;
							case 2:
								url =
									`androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
								break;
							default:
								break;
						}
						if (url != "") {
							url = encodeURI(url);
							plus.runtime.openURL(url, function(e) {
								plus.nativeUI.alert("未安装应用");
							});
						}
					})
				} else {
					// iOS上获取本机是否安装了百度高德地图，需要在manifest里配置
					// 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
					//（如urlschemewhitelist:["iosamap","baidumap"]）  
					plus.nativeUI.actionSheet({
						title: "选择地图应用",
						cancel: "取消",
						buttons: [{
							title: "百度地图"
						}, {
							title: "高德地图"
						}]
					}, function(e) {
						switch (e.index) {
							case 1:
								url =
									`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
								break;
							case 2:
								url =
									`iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
								break;
							default:
								break;
						}
						if (url != "") {
							url = encodeURI(url);
							plus.runtime.openURL(url, function(e) {
								plus.nativeUI.alert("未安装应用");
							});
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.station-list {
		position: relative;
		top: 60rpx;
		padding: 20rpx;

		.station-item {
			border-radius: 20rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx;
			margin-top: 20rpx;

			.image {
				width: 120rpx;
				height: 120rpx;
			}

			.info {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				flex-direction: column;
				width: 420rpx;


				.title {
					color: #333333;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.address {
					color: #999999;
					font-weight: 400;
					margin-bottom: 10rpx;
				}

				.detail {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.price {
						color: #333333;
						font-weight: bold;
					}

					.youhui {
						display: flex;
						align-items: center;
						justify-content: center;
						color: #005EFF;
						font-size: 24rpx;
						margin-left: 10rpx;

						.icon {
							width: 36rpx;
							height: 36rpx;
						}

						.youhui-price {
							padding: 0 4rpx;
							height: 36rpx;
							border: 2rpx solid #005EFF;
						}
					}

					.huaxian {
						color: #9f9f9f;
						font-size: 26rpx;
						margin-left: 10rpx;
						text-decoration: line-through;
					}
				}
			}

			.to {
				color: #666666;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;

				.btn {
					padding: 10rpx 30rpx;
					background: linear-gradient(180deg, #218BFF 0%, #3064FC 100%);
					color: #FFFFFF;
					border-radius: 40rpx;
					text-align: center;
					margin-bottom: 20rpx;
				}
			}
		}
	}

	.slot-content {
		padding: 0 28rpx 28rpx;
		background-color: #FFFFFF;

		.lable {
			font-size: 28rpx;
			font-family: Segoe UI;
			font-weight: bold;
			color: #333333;
			padding: 20rpx 20rpx 0;
		}

		.typeList {
			padding: 0 20rpx;
			display: flex;
			flex-wrap: wrap;
			align-items: center;

			.typeItem {
				margin-top: 32rpx;
				margin-right: 46rpx;
				width: 128rpx;
				height: 58rpx;
				background: #F2F2F2;
				border-radius: 4px;
				text-align: center;
				line-height: 58rpx;

				&:nth-child(4n) {
					margin-right: 0rpx;
				}

				&.active {
					background: linear-gradient(180deg, #218BFF 0%, #3064FC 100%);
					color: #FFFFFF;
				}
			}
		}
	}

	/deep/ .u-dropdown__menu__item__text {
		color: rgb(96, 98, 102) !important;
	}
</style>
